package com.agileai.portal.controller.wcm;

import java.util.List;

import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.controller.core.SimpleHandler;
import com.agileai.hotweb.domain.TreeBuilder;
import com.agileai.hotweb.domain.TreeModel;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.portal.bizmoduler.ColumnTreeManage;
import com.agileai.util.StringUtil;

/**
 * generated by miscdp
 */
public class ColumnProviderHandler extends SimpleHandler{
	private String DefalultColumnIdKey = "columnId";
	public static final class ListType{
		public static final String flat = "flat";
		public static final String cascade = "cascade";
	}
	private static final String NEW_LINE = "\r\n";
	
	public ColumnProviderHandler(){
		super();
	}
	
	public ViewRenderer prepareDisplay(DataParam param) {
		String columnParentId =  param.get("columnParentId");
		String columnIdKey = param.get("columnIdKey",DefalultColumnIdKey);
		String listType = param.get("listType");
		String currentColumnId =  param.get(columnIdKey);
		if (StringUtil.isNotNullNotEmpty(columnParentId)){
			ColumnTreeManage service = this.lookupService(ColumnTreeManage.class);
	        List<DataRow> menuRecords = service.findTreeRecords(param);
	        TreeBuilder treeBuilder = new TreeBuilder(menuRecords, "COL_ID","COL_NAME", "COL_PID");
	        TreeModel fullTreeModel = treeBuilder.buildTreeModel();
	        TreeModel targetModel = fullTreeModel.getFullTreeMap().get(columnParentId);
	        TreeModel selectModel = fullTreeModel.getFullTreeMap().get(currentColumnId);
	        List<TreeModel> modelList = targetModel.getChildren();
	        
	        if (ListType.cascade.equals(listType)){
	        	StringBuffer temp = new StringBuffer();
	        	this.buildCascadeHtml(selectModel, modelList, true, temp,columnIdKey);
	 	       this.setAttribute("content",temp.toString());
	        }else{
	        	String html = this.buildFlatHtml(selectModel, modelList,columnIdKey);
	        	this.setAttribute("content",html);
	        }
		}
		return new LocalRenderer(getPage());
	}

	private String buildFlatHtml(TreeModel currentItem,List<TreeModel> menuItemList,String columnIdKey){
		String result = "";
		StringBuffer temp = new StringBuffer();
		temp.append("<div>").append(NEW_LINE);
		temp.append("	<ul>").append(NEW_LINE);
		for (int i=0;i < menuItemList.size();i++){
			TreeModel treeModel = menuItemList.get(i);
			String tempColumnId = treeModel.getId();
			String tempColumnName = treeModel.getName();
			if (currentItem != null && currentItem.getId() != null && currentItem.getId().equals(tempColumnId)){
				temp.append("		<li class='selected'><a  hidefocus='true' href=\"javascript:doAjaxRefreshPortalPage('").append(columnIdKey).append(":").append(tempColumnId).append("')\" >").append(tempColumnName).append("</a></li>").append(NEW_LINE);
			}else{
				temp.append("		<li><a hidefocus='true' href=\"javascript:doAjaxRefreshPortalPage('").append(columnIdKey).append(":").append(tempColumnId).append("')\" >").append(tempColumnName).append("</a></li>").append(NEW_LINE);				
			}
		}
		temp.append("	</ul>").append(NEW_LINE);
		temp.append("</div>").append(NEW_LINE);
		result = temp.toString();
		return result;
	}
	
	private void buildCascadeHtml(TreeModel selectedModel,List<TreeModel> treeModelList,boolean topLevel,StringBuffer temp,String columnIdKey){
		if (topLevel){
			temp.append("	<ul id='accordion'>").append(NEW_LINE);
		}else{
			temp.append("	<ul>").append(NEW_LINE);	
		}
		for (int i=0;i < treeModelList.size();i++){
			TreeModel tempItem = treeModelList.get(i);
			String tempColumnId = tempItem.getId();
			String tempColumnName = tempItem.getName();
			
			if (tempItem.getChildren() != null && !tempItem.getChildren().isEmpty()){
				temp.append("		<li><a class='item' href=''>").append(tempColumnName).append("</a>").append(NEW_LINE);
				List<TreeModel> childMenuItemList = tempItem.getChildren();
				this.buildCascadeHtml(selectedModel, childMenuItemList,false,temp,columnIdKey);
				temp.append("	    </li>").append(NEW_LINE);		
			}else{
				if (topLevel){
					if (selectedModel != null && selectedModel.getId() != null && selectedModel.getId().equals(tempColumnId)){
						temp.append("		<li><a class='item selected' href=\"javascript:doAjaxRefreshPortalPage('").append(columnIdKey).append(":").append(tempColumnId).append("')\" >").append(tempColumnName).append("</a></li>").append(NEW_LINE);
					}else{
						temp.append("		<li><a class='item' href=\"javascript:doAjaxRefreshPortalPage('").append(columnIdKey).append(":").append(tempColumnId).append("')\" >").append(tempColumnName).append("</a></li>").append(NEW_LINE);				
					}						
				}else{
					if (selectedModel != null && selectedModel.getId() != null && selectedModel.getId().equals(tempColumnId)){
						temp.append("		<li><a class='selected' href=\"javascript:doAjaxRefreshPortalPage('").append(columnIdKey).append(":").append(tempColumnId).append("')\" >").append(tempColumnName).append("</a></li>").append(NEW_LINE);
					}else{
						temp.append("		<li><a href=\"javascript:doAjaxRefreshPortalPage('").append(columnIdKey).append(":").append(tempColumnId).append("')\" >").append(tempColumnName).append("</a></li>").append(NEW_LINE);				
					}				
				}
			}
		}
		temp.append("	</ul>").append(NEW_LINE);
	}
}
